package com.yuezhu.Model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yuezhu.been.UserMsg;
import com.yuezhu.global.ApplicationContext;
import com.yuezhu.util.Preference;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/6/12.
 */
public class DictionaryModel {
    private ArrayList<Type> typeList = null;
    private ArrayList<Grade> gradeList = null;
    private ArrayList<LiftLoadModel> liftLoadModelArrayList = null;
    private ArrayList<LiftSpeedModel> liftSpeedModelArrayList = null;
    private static String GRADE_JSON = "[{\"createAt\":1464072932000,\"gradeId\":2,\"typeId\":4,\"gradeName\":\"乙级\",\"targetTime\":40,\"targetLoad\":1000,\"targetPerson\":10,\"targetHc\":10,\"targetInt\":50},{\"createAt\":1464073828000,\"gradeId\":3,\"typeId\":4,\"gradeName\":\"甲级\",\"targetTime\":30,\"targetLoad\":1350,\"targetPerson\":12,\"targetHc\":10,\"targetInt\":45},{\"createAt\":1464073876000,\"gradeId\":4,\"typeId\":4,\"gradeName\":\"超甲\",\"targetTime\":25,\"targetLoad\":1600,\"targetPerson\":15,\"targetHc\":12,\"targetInt\":40},{\"createAt\":1464074108000,\"gradeId\":5,\"typeId\":5,\"gradeName\":\"现代\",\"targetTime\":35,\"targetLoad\":1000,\"targetPerson\":2,\"targetHc\":10,\"targetInt\":55},{\"createAt\":1464074153000,\"gradeId\":6,\"typeId\":5,\"gradeName\":\"舒适\",\"targetTime\":30,\"targetLoad\":1350,\"targetPerson\":2,\"targetHc\":11,\"targetInt\":45},{\"createAt\":1464074187000,\"gradeId\":7,\"typeId\":5,\"gradeName\":\"豪华\",\"targetTime\":25,\"targetLoad\":1600,\"targetPerson\":2,\"targetHc\":12,\"targetInt\":35},{\"createAt\":1464074244000,\"gradeId\":8,\"typeId\":6,\"gradeName\":\"经济\",\"targetTime\":40,\"targetLoad\":1000,\"targetPerson\":25,\"targetHc\":5,\"targetInt\":90},{\"createAt\":1464074279000,\"gradeId\":9,\"typeId\":6,\"gradeName\":\"中档\",\"targetTime\":35,\"targetLoad\":1000,\"targetPerson\":35,\"targetHc\":6,\"targetInt\":80},{\"createAt\":1464074322000,\"gradeId\":10,\"typeId\":6,\"gradeName\":\"高档\",\"targetTime\":30,\"targetLoad\":1350,\"targetPerson\":45,\"targetHc\":7,\"targetInt\":70}]";
    private static String TYPE_JSON = "[{\"createAt\":1464068962000,\"typeId\":4,\"typeName\":\"办公\"},{\"createAt\":1464068980000,\"typeId\":5,\"typeName\":\"酒店\"},{\"createAt\":1464068989000,\"typeId\":6,\"typeName\":\"住宅\"}]";

    public class Type {
        public long createAt;
        public int typeId;
        public String typeName;
    }

    public class Grade {
        public long createAt;
        public int gradeId;
        public int typeId;
        public String gradeName;
        public int targetTime;
        public int targetLoad;
        public int targetPerson;
        public int targetHc;
        public int targetInt;
    }

    private static DictionaryModel instance;

    public static DictionaryModel getInstance() {
        if (instance == null) {
            instance = new DictionaryModel();
        }
        return instance;
    }

    private DictionaryModel() {
        Gson gson = new Gson();
        try {
            gradeList = gson.fromJson(Preference.instance(ApplicationContext.getContext()).getString(Preference.DICTIONARY_MODEL_GRADE_JSON), new TypeToken<ArrayList<Grade>>() {
            }.getType());
            typeList = gson.fromJson(Preference.instance(ApplicationContext.getContext()).getString(Preference.DICTIONARY_MODEL_TYPE_JSON), new TypeToken<ArrayList<Type>>() {
            }.getType());
            liftLoadModelArrayList = gson.fromJson(Preference.instance(ApplicationContext.getContext()).getString(Preference.DICTIONARY_LIFT_LOAD_JSON), new TypeToken<ArrayList<LiftLoadModel>>() {
            }.getType());
            liftSpeedModelArrayList = gson.fromJson(Preference.instance(ApplicationContext.getContext()).getString(Preference.DICTIONARY_LIFT_SPEED_JSON), new TypeToken<ArrayList<LiftSpeedModel>>() {
            }.getType());
        } catch (Exception e) {
            //默认值
            gradeList = gson.fromJson(GRADE_JSON, new TypeToken<ArrayList<Grade>>() {
            }.getType());
            typeList = gson.fromJson(TYPE_JSON, new TypeToken<ArrayList<Type>>() {
            }.getType());
            liftSpeedModelArrayList = new ArrayList<>();
            liftLoadModelArrayList = new ArrayList<>();
        }
        if (gradeList == null) {
            gradeList = gson.fromJson(GRADE_JSON, new TypeToken<ArrayList<Grade>>() {
            }.getType());
        }
        if (typeList == null) {
            typeList = gson.fromJson(TYPE_JSON, new TypeToken<ArrayList<Type>>() {
            }.getType());
        }
        if (liftSpeedModelArrayList == null)
            liftSpeedModelArrayList = new ArrayList<>();
        if (liftLoadModelArrayList == null)
            liftLoadModelArrayList = new ArrayList<>();
    }

    public ArrayList<String> getLiftSpeed(double speed) {
        ArrayList<String> strings = new ArrayList<>();
        for (LiftSpeedModel liftSpeedModel : liftSpeedModelArrayList) {
            if (UserMsg.getInstance().getLevel() == 1 || UserMsg.getInstance().getLevel() == liftSpeedModel.flagBasic) {
                strings.add(liftSpeedModel.speedSec + "");
            } else if (speed == liftSpeedModel.speedSec) {
                strings.add(liftSpeedModel.speedSec + "");
            }
        }
        return strings;
    }

    public ArrayList<String> getLiftLoad(int loadKg) {
        ArrayList<String> strings = new ArrayList<>();
        for (LiftLoadModel liftLoadModel : liftLoadModelArrayList) {
            if (UserMsg.getInstance().getLevel() == 1 || UserMsg.getInstance().getLevel() == liftLoadModel.flagBasic) {
                strings.add(liftLoadModel.loadKg + "(" + liftLoadModel.loadPerson + "人)");
            } else if (loadKg == liftLoadModel.loadKg) {
                strings.add(liftLoadModel.loadKg + "(" + liftLoadModel.loadPerson + "人)");
            }
        }
        return strings;
    }

    public Type getTypeByTypeId(int id) {
        for (Type type : typeList) {
            if (type.typeId == id) {
                return type;
            }
        }
        return null;
    }

    public int getTypeIDByTypeNmae(String name) {
        for (Type type : typeList) {
            if (type.typeName.equalsIgnoreCase(name)) {
                return type.typeId;
            }
        }
        return -1;
    }

    public Grade getGradeByGradeId(int id) {
        for (Grade grade : gradeList) {
            if (grade.gradeId == id) {
                return grade;
            }
        }
        return null;
    }

    public int getGradeIdByGradeName(String name) {
        for (Grade grade : gradeList) {
            if (grade.gradeName.equalsIgnoreCase(name)) {
                return grade.gradeId;
            }
        }
        return -1;
    }

    public ArrayList<Grade> getGradeListByType(Type type) {
        ArrayList<Grade> list = new ArrayList<>();
        for (Grade grade : gradeList) {
            if (type.typeId == grade.typeId) {
                list.add(grade);
            }
        }
        return list;
    }

    public ArrayList<String> getGradeNameListByTypeName(String name) {
        for (Type type : typeList) {
            if (type.typeName.equalsIgnoreCase(name)) {
                ArrayList<Grade> gList = getGradeListByType(type);
                ArrayList<String> list = new ArrayList<>();
                for (Grade grade : gList) {
                    list.add(grade.gradeName);
                }
                return list;
            }
        }
        return new ArrayList<>();
    }

    public ArrayList<String> getTypeNameList() {
        ArrayList<String> list = new ArrayList<>();
        for (Type type : typeList) {
            list.add(type.typeName);
        }
        return list;
    }
}
